package util;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

public class FileUtil {

	/**
	 * 取得文件扩展名
	 * @parma fullFileName 文件路径+文件名称 文件路径+文件名称或者文件名 如:D:\\test\\test.jsp 或者/test/test.jsp 或者test.jsp
	 * @return String
	 */
	public static String getFileExtName(String fullFileName) {
		int i = 0, Len = 0;
		String charStr = "", rtn = "";

		if (fullFileName == null)
			return "";
		fullFileName = fullFileName.trim();
		Len = fullFileName.length();
		if (Len <= 1)
			return "";

		for (i = Len - 1; i > 0; i--) {
			charStr = fullFileName.substring(i, i + 1);
			rtn = charStr + rtn;
			if (charStr.compareTo(".") == 0)
				break;
		}
		if (rtn.length() > 5)
			return "";
		else
			return rtn;
	}
	
	/**
	 * 创建Web目录的方法
	 * @param request:Http回应request类
	 * @param dirs:Web路径 /相对路径 相对于跟目录 例如/Document/Contract/
	 * @return true/false
	 * @throws Exception
	 */
	public boolean makeRemoteDir(HttpServletRequest request, String dirs)
			throws Exception {
		boolean result = false;
		if (dirs != null) {
			String pathString = "";
			//得到绝对路径
			pathString = request.getSession().getServletContext().getRealPath("");
			pathString = pathString.replace('\\', '/');
			//得到目录路径
			pathString = pathString + dirs;
			try {
				File fi = new File(pathString);
				//创建目录
				result = fi.mkdirs();
			} catch (Exception e) {
				result = false;
				System.err.println(e.getMessage());
			}
		}
		return result;
	}
}
